package top.fengleifeng.question;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import top.fengleifeng.util.Constants;

/**
 * @author : fengelfeing
 * @version V1.0
 * @Project: leetcode-test
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年11月02日3:19 下午
 */
public class Num349两个数组的交集 {

    //用hashmap
    class Solution {

        public int[] intersection(int[] nums1, int[] nums2) {
            Set<Integer> set1 = new HashSet<>();
            for (int i = 0; i < nums1.length; i++) {
                set1.add(nums1[i]);
            }
            Set<Integer> set2 = new HashSet<>();
            for (int i = 0; i < nums2.length; i++) {
                set2.add(nums2[i]);
            }
            Set<Integer> set3 = new HashSet<>();
            for (Integer i : set1
            ) {
                if (set2.contains(i)) {
                    set3.add(i);
                }
            }
            int[] result = new int[set3.size()];
            int ii = 0;
            for (Integer i : set3) {
                result[ii++] = i;
            }
            return result;
        }
    }

    public static void main(String[] args) {
        int[] ints = {1,2,2,1};
        int[] ints1 = {2,2};
        int[] intersection = new Num349两个数组的交集().new Solution().intersection(ints, ints1);
        Constants.printArr(intersection);
    }

}
